/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.util.Hashtable;

/**
 *
 * @author Kvest
 */
public class ActionManager
{
    private static ActionManager manager;

    //Коллекция для хранения обработчиков
    private Hashtable actions;

    private Hashtable changes;
    private Hashtable itemNames;
    
    private ActionManager()
    {
        actions = new Hashtable();
        changes = new Hashtable();
        itemNames = new Hashtable();
    }

    public static ActionManager getManager()
    {
        if (manager == null)
        {
            manager = new ActionManager();
        }

        return manager;
    }

    public void addAction(String name,ActionCallback callback)
    {
        actions.put(name, callback);
    }

    public ActionCallback getAction(String name)
    {
        if (actions.containsKey(name))
        {
            return (ActionCallback)actions.get(name);
        }
        else
        {
            return null;
        }
    }

    public void addItemChangeCallback(String name, String text, ItemChangeCallback callback)
    {
        changes.put(name, callback);
        itemNames.put(name, text);
    }

    public boolean addText(String name, String delta)
    {
        if (itemNames.containsKey(name))
        {
            return setText(name, (String)itemNames.get(name) + delta);
        }
        else
        {
            return false;
        }
    }
    
    public boolean setText(String name, String value)
    {
        if (changes.containsKey(name))
        {
            //Делаем изменения
            ItemChangeCallback callback = (ItemChangeCallback)changes.get(name);
            callback.change(value);

            return true;
        }
        else
        {
            return false;
        }
        
    }


    public void removeAction(String name)
    {
        actions.remove(name);
    }

    public interface ActionCallback
    {
        public abstract void action();
    }

    public interface ItemChangeCallback
    {
        public abstract void change(String text);
    }
}
